package com.example.design.bridge.abst.factory;

import com.example.design.bridge.abst.AbstractRegisterLoginComponent;
import com.example.design.bridge.abst.RegisterLoginComponent;
import com.example.design.bridge.function.AbstractRegisterLoginFunc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*
* 工厂模式
* */
public class RegisterLoginComponentFactory {

    //缓存 AbstractRegisterLoginComponent (左路)。根据不同登录方式进行缓存。
    public static final Map<String, AbstractRegisterLoginComponent> componentMap =new ConcurrentHashMap<>();

    //缓存不同类型的实现类。如RegisterLoginByGitee、RegisterLoginByDefault
    public static Map<String, AbstractRegisterLoginFunc> funcMap =new ConcurrentHashMap<>();

    public static AbstractRegisterLoginComponent getRegisterLoginComponent(String loginType)
    {
        AbstractRegisterLoginComponent component = componentMap.get(loginType);
        if (component == null){
            //并发情况下，汲取双重检查锁机制的设计，如果缓存中没有，则创建。
            synchronized (componentMap){
                component = componentMap.get(loginType);
                if (component == null){
                    component = new RegisterLoginComponent(funcMap.get(loginType));
                    componentMap.put(loginType,component);
                }
            }
        }
        return component;
    }


}
